<template>
	<view class="marquee">
		<slot name="left"></slot>
		<view :id="id1" class="marquee-parent" :style="{'width': width, 'align-items': align, 'justify-content': realJustify}">
			<view :id="id2" class="marquee-content" :class="[className]" :style="{'animation-duration': `${duration}s`, 'animation-delay': `${delay}s`}">
				<slot></slot>
			</view>
		</view>
		<slot name="right"></slot>
	</view>
</template>

<script>
	function guid() {
	    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
	        var r = Math.random() * 16 | 0,
	            v = c == 'x' ? r : (r & 0x3 | 0x8);
	        return v.toString(16);
	    });
	}
	export default {
		data() {
			return {
				sWidth: 0,
				cWidth: 0,
				id1: 'marquee-parent-' + guid(),
				id2: 'marquee-content-' + guid(),
				realJustify: 'justify',
				duration: 10,
				computeCounter: 0
			}
		},
		props: {
			height: {
				type: String,
				default: '38rpx'
			},
			width: {
				type: String,
				default: '200rpx'
			},
			align: {
				type: String,
				default: 'center'
			},
			justify: {
				type: String,
				default: 'left'
			},
			speed: {
				type: Number,
				default: 10 //每秒10个像素
			},
			delay: {
				type: Number,
				default: 0
			},
			animationType: {
				type: String,
				default: 'back', // reverse: 结束后倒着执行，back：结束后回到开头
			}
		},
		mounted() {
			this.realJustify = this.justify
			this.updateSize()
		},
		updated() {
			this.updateSize()
		},
		computed: {
			className() {
				if (this.cWidth == 0) {
					return ''
				}
				if (this.justify == 'center' && this.sWidth > this.cWidth) {
					this.realJustify = 'left'
				}
				let diff = this.sWidth - this.cWidth
				var t = diff / this.speed
				if (t < 10) {
					t = 10
				}
				this.duration = t
				// console.log('duration', this.duration)
				var num = 0
				if (this.realJustify == 'left') {
					num = Math.round( diff / this.sWidth * 100)
				} else if (this.realJustify == 'center') {
					num = Math.round(diff * 0.5 / this.sWidth * 100)
				} else if (this.realJustify == 'right') {
					//? 暂时不知道怎么算
					// num = Math.round((this.sWidth - this.cWidth) / this.sWidth * 100 / 2)
				}
				// console.log('xxx', num, this.sWidth, this.cWidth)
				if (num <= 0) {
					return ''
				}
				if (this.animationType == 'reverse') {
					return `rolling-reverse-${num}`
				} else if (this.animationType == 'back') {
					return `rolling-back-${num}`
				}
				return ''
			}
		},
		methods: {
			updateSize() {
				if (this.computing) {
					return
				}
				let pages = getCurrentPages()
				// console.log('pages', pages.length)
				if (!pages.length) {
					// 当pages的长度为0是不执行，当切换页面的时候pages的长度可能为0，原因不明
					return
				}
				this.query = uni.createSelectorQuery().in(this)
				this.nodeRef1 = this.query.select(`#${this.id1}`)
				this.nodeRef2 = this.query.select(`#${this.id2}`)
				this.computing = true
				// console.log('start compute')
				this.nodeRef1.boundingClientRect(data => {
					this.cWidth = data.width
					this.computeCounter += 1
					if (this.computeCounter == 2) {
						this.computeCounter = 0
						this.computing = false
						// console.log('finish compute')
					}
				}).exec();
				this.nodeRef2.boundingClientRect(data => {
					this.sWidth = data.width
					this.computeCounter += 1
					if (this.computeCounter == 2) {
						this.computeCounter = 0
						this.computing = false
						// console.log('finish compute')
					}
				}).exec();
			}
		}
	}
</script>

<style scoped lang="scss">
	.marquee {
		display: flex;
		align-items: center;
	}
	.marquee-parent {
		overflow-x: scroll;
		white-space: nowrap;
		display: flex;
	}
	.marquee-content {
		display: inline-flex;
	}

	@for $i from 1 through 100 {
		.rolling-back-#{$i} {
			animation-name: sport-back-#{$i};
			animation-timing-function: linear;
			animation-iteration-count: infinite;
		}

		@keyframes sport-back-#{$i} {
			0% {
				transform: translateX(0%);
			}
			30% {
				transform: translateX(0%);
			}
			85% {
				transform: translateX(#{-$i + '%'});
			}
			98% {
				transform: translateX(#{-$i + '%'});
			}
			100% {
				transform: translateX(0);
			}
		}
	}
	
	@for $i from 1 through 100 {
		.rolling-reverse-#{$i} {
			animation-name: sport-reverse-#{$i};
			animation-timing-function: linear;
			animation-iteration-count: infinite;
		}
	
		@keyframes sport-reverse-#{$i} {
			0% {
				transform: translateX(0%);
			}
			30% {
				transform: translateX(0%);
			}
			50% {
				transform: translateX(#{-$i + '%'});
			}
			80% {
				transform: translateX(#{-$i + '%'});
			}
			100% {
				transform: translateX(0%);
			}
		}
	}
</style>
